package org.vectrics.recruiter.struts.tag;

import org.apache.ecs.html.Div;
import org.apache.ecs.html.Font;
import org.apache.ecs.html.IMG;
import org.apache.ecs.html.Input;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.StringUtils;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;

/**
 * @author pgoette
 * Tag encargada de mostrar las location.
 */
public class LocationPickerTag extends VectricsTagSupport {

	private static final long serialVersionUID = -1274812638281149245L;
	
	private String typeCode;
	private String popupLeft;
	private String popupTop;
	private String caption;
	private String paramName;
	private String style;
	private String styleClase;
    private String canSelecction = null;

	/**
	 * @return the styleClase
	 */
	public String getStyleClase() {
		return styleClase;
	}

	/**
	 * @param styleClase the styleClase to set
	 */
	public void setStyleClase(String styleClase) {
		this.styleClase = styleClase;
	}

	/**
	 * @return the popupLeft
	 */
	public String getPopupLeft() {
		return popupLeft;
	}

	/**
	 * @param popupLeft the popupLeft to set
	 */
	public void setPopupLeft(String popupLeft) {
		this.popupLeft = popupLeft;
	}

	/**
	 * @return the popupTop
	 */
	public String getPopupTop() {
		return popupTop;
	}

	/**
	 * @param popupTop the popupTop to set
	 */
	public void setPopupTop(String popupTop) {
		this.popupTop = popupTop;
	}

	/**
	 * @return the caption
	 */
	public String getCaption() {
		return caption;
	}

	/**
	 * @param caption the caption to set
	 */
	public void setCaption(String caption) {
		this.caption = caption;
	}

	/**
	 * @return the paramName
	 */
	public String getParamName() {
		return paramName;
	}

	/**
	 * @param paramName the paramName to set
	 */
	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	/**
	 * @return the style
	 */
	public String getStyle() {
		return style;
	}

	/**
	 * @param style the style to set
	 */
	public void setStyle(String style) {
		this.style = style;
	}


	/**
	 * @return the typeCode
	 */
	public String getTypeCode() {
		return typeCode;
	}

	/**
	 * @param typeCode the typeCode to set
	 */
	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}

	/**
	 * @return the canSelecction
	 */
	public String getCanSelecction() {
		return canSelecction;
	}

	/**
	 * @param canSelecction the canSelecction to set
	 */
	public void setCanSelecction(String canSelecction) {
		this.canSelecction = canSelecction;
	}

	@Override
	protected String makeStartTag() {
		String inputId = "location_picker_" + paramName;
		// Creo la tabla
		Table table = new Table();
    	table.setCellPadding(0);
    	table.setCellSpacing(0);
    	Row innerRow = table.addRow();
    	Cell innerCell1 = innerRow.addCell();
    	Cell innerCell2 = innerRow.addCell();
    	// Creo la div
    	Div innerDiv = new Div();
    	
    	if (style!=null) {
    		innerDiv.setStyle(style);    		
    	}
    	if (styleClase!=null) {
    		innerDiv.setClass(styleClase);    		
    	}
    	innerDiv.setID(inputId);
    	innerCell1.addElement(innerDiv);
    	// Este es el input donde se guarda el valor, ya que en pantalla se muestra con una div. 
    	Input hidden = new Input();
        hidden.setType(Input.HIDDEN);
        hidden.setName(paramName);
        hidden.setValue("");
        hidden.setID(paramName);
        // Toma la location por defecto de la request.
        String locationId =  this.getRequest().getParameter(paramName);
        if (StringUtils.isNullOrBlank(locationId)) {
        	if (this.getRequest().getAttribute(paramName) != null){
        		locationId = this.getRequest().getAttribute(paramName).toString();
        	} 
        }

        if (!StringUtils.isNullOrBlank(this.getRequest().getParameter("reset"))) {
        	locationId = "";
        }
        
        // Si el id no es nulo lo agrego a la div.
        if (!StringUtils.isNullOrBlank(locationId)) {
        	Location location = CoreServiceLocator.getLocationService().findLocation(new Long(locationId));
        	Font font = new Font();
        	font.setClass("stdFont");
        	font.addElement(location.getName());
        	hidden.setValue(locationId);
        	innerDiv.addElement(font);
        } 
        
        innerCell1.addElement(hidden);
        
        if (this.getCanSelecction()==null){
        	this.setCanSelecction("country");
        }
        
        String strCaption = this.getPhrase(this.getCaption());
        
	    // Button to pick location
    	IMG buttonImage = new IMG();
    	buttonImage.setSrc("/hrs-intranet/dynagraphic/button.jpg?code=button.date.pick&caption=Select");
    	buttonImage.setAlt("Pick Location");
    	buttonImage.setBorder(0);
    	buttonImage.setClass("imageButton");
    	buttonImage.setOnClick("javascript:locationSearchDiv.showLocationSearchDiv(" +
    			popupLeft + ", " +
    			popupTop + ", '" +
    			strCaption + "', '" +
    			paramName + "', '" +
    			typeCode   + "', '" +
    			canSelecction   +	"');");

    	innerCell2.addElement(buttonImage);
    	
    	return table.toString();

	}
	

}
